package com.ruoyi.workflow.controller;

import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.workflow.mapper.BpmnInfoMapper;
import org.apache.commons.io.IOUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.workflow.domain.BpmnInfo;
import com.ruoyi.workflow.service.IBpmnInfoService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 流程定义明细Controller
 * 
 * @author ruoyi
 * @date 2022-11-14
 */
@RestController
@RequestMapping("/workflow/bpmnInfo")
public class BpmnInfoController extends BaseController
{
    @Autowired
    private IBpmnInfoService bpmnInfoService;
    @Autowired
    private BpmnInfoMapper bpmnInfoMapper;

    public static List<String> SUPPORT_SUFFIXS = Arrays.asList("bpmn", "xml", "zip");

    /**
     * 查询流程定义明细列表
     */
    @PreAuthorize("@ss.hasPermi('business:bpmnlnfo:list')")
    @GetMapping("/list")
    public TableDataInfo list(BpmnInfo bpmnInfo)
    {
        startPage();
        List<BpmnInfo> list = bpmnInfoService.selectBpmnInfoList(bpmnInfo);
        return getDataTable(list);
    }



    /**
     * 删除流程定义明细
     */
    @PreAuthorize("@ss.hasPermi('business:bpmnlnfo:remove')")
    @Log(title = "流程定义明细", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(bpmnInfoService.delDefinition(ids));
    }
    @PreAuthorize("@ss.hasPermi('business:bpmnInfo:deploy')")
    @Log(title = "流程定义部署", businessType = BusinessType.INSERT)
    @PostMapping("/deploy")
    public AjaxResult deploy(MultipartFile file, BpmnInfo bpmnInfo) throws Exception {
        // 判断文件的类型是否是指定的类型
        // 获取到原始的文件名
        // 将文件名的后存到BpmnInfo 对象中，
        String resourceFileName = file.getOriginalFilename();
        // 获取到文件的后缀
        String suffix = FileUtils.getSuffix(resourceFileName);
        if (!SUPPORT_SUFFIXS.contains(suffix)) {
            return AjaxResult.error("目前只支持" + SUPPORT_SUFFIXS + "文件类型");
        }
        bpmnInfo.setResourceName(resourceFileName);
        bpmnInfoService.deploy(bpmnInfo, file.getInputStream());
        return AjaxResult.success("部署成功");
    }

    /**
     * 查看流程文件或流程图片
     * @param id
     * @param type
     * @param resp
     * @throws IOException
     */
    @PreAuthorize("@ss.hasPermi('business:bpmnlnfo:view')")
    @GetMapping("/resource/{id}")
    public void getResource(@PathVariable Long id, String type, HttpServletResponse resp) throws IOException {
        System.out.println(id);
        // 文件获取就是从输入到输出
        InputStream inputStream =  bpmnInfoService.getResourceTypeById(id, type);
        IOUtils.copy(inputStream, resp.getOutputStream());
    }
}
